﻿using rmTest.Common.ModelUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace rmTest.Common.AttrUtil
{
    /// <summary>
    /// 1、援引MSDN
    ///【
    ///特性类的定位参数和命名参数的类型仅限于特性参数类型，这些包括：
    ///bool, byte, char, double, float, int, long, short, string, System.Type, object, enum。
    ///】
    ///而 DateTime 是一个 struct，不属于上面类型或其派生类型。
    ///可为空的类型也不能用
    ///2、特性参数的值必须是一个编译时常量，即在编译的时候编译器可以把他们等量代换成一个常数值。DateTime.Now 显然不是。
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class ModelValidateAttribute : ModelBaseValidateAttribute
    {
        public ModelValidateAttribute() { }

        public ModelValidateAttribute(bool required, bool ignored = false, bool isPrimaryKey = false, int minCharLength = int.MaxValue, int maxCharLength = int.MinValue, float minRange = float.MaxValue, float maxRange = float.MinValue, string numberFormat = null, string dateFormat = null) : base(ignored, required, isPrimaryKey)
        {
            this.MinCharLength = minCharLength;
            this.MaxCharLength = maxCharLength;
            this.MinRange = minRange;
            this.MaxRange = maxRange;
            this.NumberFormat = numberFormat;
            this.DateFormat = dateFormat;
        }

        private int _minCharLength = int.MaxValue;
        /// <summary>
        /// 字符长度最小值
        /// </summary>
        public int MinCharLength
        {
            get
            {
                return this._minCharLength;
            }
            set
            {
                this._minCharLength = value;
            }
        }
        private int _maxCharLength = int.MinValue;
        /// <summary>
        /// 字符长度最大值
        /// </summary>
        public int MaxCharLength
        {
            get
            {
                return this._maxCharLength;
            }
            set
            {
                this._maxCharLength = value;
            }
        }

        private float _minRange = float.MaxValue;
        /// <summary>
        /// 数值最小值
        /// </summary>
        public float MinRange
        {
            get
            {
                return this._minRange;
            }
            set
            {
                this._minRange = value;
            }
        }
        private float _maxRange = float.MinValue;
        /// <summary>
        /// 数值最大值
        /// </summary>
        public float MaxRange
        {
            get
            {
                return this._maxRange;
            }
            set
            {
                this._maxRange = value;
            }
        }
        /// <summary>
        /// 数字显示格式
        /// </summary>
        public string NumberFormat { get; set; }
        /// <summary>
        /// 时间/日期显示格式
        /// </summary>
        public string DateFormat { get; set; }

        public override void Validate<T>(ModelBaseValidateAttribute attribute, T t, ValidateResult result)
        {
            var attr = attribute as ModelValidateAttribute;
            var key = result.ValidateMsg.Keys.Last<string>();
            StringBuilder builder = new StringBuilder();
            //必填检验
            if (attr.Required)
            {
                if (t == null || string.IsNullOrEmpty(t.ToString()))
                {
                    result.isValidated = false;
                    builder.Append("属性必填验证不通过！");
                    if (!result.isValidateAll)
                    {
                        result.ValidateMsg[key] = builder.ToString();
                        return;
                    }
                }
            }
            if (attr.MinCharLength != int.MaxValue)
            {
                if (attr.MinCharLength > t.ToString().Length)
                {
                    result.isValidated = false;
                    builder.AppendFormat("属性长度小于指定最小长度，指定最小长度：{0}，当前值：{1}，长度：{2}！", attr.MinCharLength, t.ToString(), t.ToString().Length);
                    if (!result.isValidateAll)
                    {
                        result.ValidateMsg[key] = builder.ToString();
                        return;
                    }
                }
            }
            if (attr.MaxCharLength != int.MinValue)
            {
                if (attr.MaxCharLength < t.ToString().Length)
                {
                    result.isValidated = false;
                    builder.AppendFormat("属性长度大于指定最大长度，指定最大长度：{0}，当前值：{1}，长度：{2}！", attr.MaxCharLength, t.ToString(), t.ToString().Length);
                    if (!result.isValidateAll)
                    {
                        result.ValidateMsg[key] = builder.ToString();
                        return;
                    }
                }
            }
            if (attr.MinRange != float.MaxValue)
            {
                float f = float.MaxValue;
                bool isFloat = float.TryParse(t.ToString(), out f);
                if (!isFloat || (f < attr.MinRange))
                {
                    result.isValidated = false;
                    builder.AppendFormat("属性值小于指定最小值，指定最小值：{0}，当前值：{1}！", attr.MinRange, t.ToString());
                    if (!result.isValidateAll)
                    {
                        result.ValidateMsg[key] = builder.ToString();
                        return;
                    }
                }
            }

            if (attr.MaxRange != float.MinValue)
            {
                float f = float.MinValue;
                bool isFloat = float.TryParse(t.ToString(), out f);
                if (!isFloat || (f > attr.MaxRange))
                {
                    result.isValidated = false;
                    builder.AppendFormat("属性值大于于指定最大值，指定最大值：{0}，当前值：{1}！", attr.MaxRange, t.ToString());
                    if (!result.isValidateAll)
                    {
                        result.ValidateMsg[key] = builder.ToString();
                        return;
                    }
                }
            }
            result.ValidateMsg[key] = builder.ToString();
        }
    }
}
